import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/page/admin/Index'
import shop from '@/page/admin/shop'
import order from '@/page/admin/order'
import user from '@/page/admin/user'
import set from '@/page/admin/set'
import goods from '@/page/admin/goods'
import create from '@/page/admin/create'
import login from '@/page/admin/login'
Vue.use(Router) //vue使用Roter

const router = new Router({
  routes: [{
      path: '/',
      redirect: '/login' //重定向跳转到指定路由
    },
    {
      path: '/login',
      name: 'login',
      component: login,
    },
    {
      path: '/index',
      name: 'index',
      component: Index
    },
    {
      path: '/shop', //访问路由地址
      name: 'shop', //路由名称
      component: shop, //该路由地址下要显示的组件
      children: [{
          path: '/shop', //子路由
          redirect: 'goods' //重定向跳转到指定路由
        },
        {
          path: 'goods',
          name: 'goods',
          component: goods,
        },
        {
          path: 'create', // 传参数 点击基础设置   不传参数 点击发布商品
          name: 'create',
          component: create,
        }
      ]
    },
    {
      path: '/order', //访问路由地址
      name: 'order', //路由名称
      component: order //该路由地址下要显示的组件
    },
    {
      path: '/user', //访问路由地址
      name: ' user', //路由名称
      component: user //该路由地址下要显示的组件
    },
    {
      path: '/set', //访问路由地址
      name: 'set', //路由名称
      component: set //该路由地址下要显示的组件
    },

  ]
})
export default router
router.beforeEach((to, from, next) => {
  if (to.path == '/login') { 
    if (!sessionStorage.token) {
      next()
    } else {
      next(from.path)
    }
  } else {
    if (sessionStorage.token) {
      next()
    } else {
      next('/login')
    }
  }
})
